package tv.youxing.android.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import tv.youxing.android.R;
import tv.youxing.android.entity.DynamicDetailsPingLunModel;
import tv.youxing.android.entity.MyMessageModel;
import tv.youxing.android.ui.DynamicDetailsActivity;
import tv.youxing.android.utils.PicassoUtils;

/**
 * Created by Administrator on 2017/6/26.
 */

public class MyMessageAdapter extends BaseAdapter {



    private Context context;
    private List<MyMessageModel.ResultBean.DataBean> listdata = new ArrayList<>();
    private LayoutInflater layoutInflater;


    public MyMessageAdapter(Context context,  List<MyMessageModel.ResultBean.DataBean> listdata) {
        this.context = context;
        this.listdata = listdata;
        layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return listdata==null || listdata.size()<=0 ? 0 : listdata.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (viewHolder==null) {
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.mymessage_item, null);


            viewHolder.rootView = convertView.findViewById(R.id.rootView);
            viewHolder.tar_ivHead = convertView.findViewById(R.id.iv_head);
            viewHolder.tar_name = convertView.findViewById(R.id.tar_name);
            viewHolder.tar_content = convertView.findViewById(R.id.tar_content);
            viewHolder.content = convertView.findViewById(R.id.content);

            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }


        viewHolder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, DynamicDetailsActivity.class);
                Bundle bundle = new Bundle();

                DynamicDetailsPingLunModel model = new DynamicDetailsPingLunModel();
                DynamicDetailsPingLunModel.PublishUserBean model_user = new DynamicDetailsPingLunModel.PublishUserBean();

                model.setContent(listdata.get(position).getContent());
                model.setPublish_time(listdata.get(position).getPublish_time());
                model.setIs_like(listdata.get(position).getIs_like());
                model.setTweet_id(listdata.get(position).getResource_id());
                model.setLike_count(listdata.get(position).getLike_count());
                model.setComment_count(listdata.get(position).getComment_count()+"");
                model_user.setAvatar(listdata.get(position).getPublish_user().getAvatar());
                model_user.setUsername(listdata.get(position).getPublish_user().getUsername());
                model.setPublish_user(model_user);
                bundle.putSerializable("data", model);
                intent.putExtras(bundle);

//                L.d("评论数:" + model.getLike_count());

                context.startActivity(intent);
            }
        });

        if (listdata.get(position).getPublish_user().getAvatar() != null && !listdata.get(position).getPublish_user().getAvatar().equals("")) {
            PicassoUtils.loadImageView(context,listdata.get(position).getPublish_user().getAvatar(),viewHolder.tar_ivHead);
        }
//        //评论用户的信息
        viewHolder.tar_name.setText(listdata.get(position).getPublish_user().getUsername().toString());
        viewHolder.tar_content.setText(listdata.get(position).getContent().toString());
        viewHolder.tar_name.setText(listdata.get(position).getPublish_user().getUsername().toString());
//
//        //发表的动态cotent
        viewHolder.content.setText(listdata.get(position).getTarget().toString());

        return convertView;
    }


    class ViewHolder{
        private LinearLayout rootView;
        private ImageView tar_ivHead;
        private TextView tar_name;
        private TextView tar_content;
        private TextView content;


    }
}
